package com.weiran.jwt.controller;

import cn.hutool.core.util.StrUtil;
import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;


@RestController
@RequestMapping("/test")
public class TestController {

    /**
     *  使用jjwt工具类来解析Authorization头中存储的JWT内容
     **/
    @GetMapping("/getCurrentTest")
    public Object getCurrentTest(Authentication authentication, HttpServletRequest request) {
        String header = request.getHeader("Authorization");
        String token = StrUtil.subAfter(header, "bearer ", false);
        return Jwts.parser()
                .setSigningKey("test_key".getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token)
                .getBody();
    }

    @GetMapping("/show")
    public String getShow() {
        return "JWT授权成功！！！";
    }

}
